const App = getApp();
// 默认未登录的灰色头像
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  data: {
    // 页面标题
    page: {},
    // 页面元素
    items: {},
    items1:{},
    items2:{},
    // 门店首页列表相关
    isAuthor: true,
    isLoading: true, // 是否正在加载中
    shopList: [] // 门店列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;
    // 页面id
    _this.data.page_id = options.page_id;
    _this.data.page_id = App.jigouPageId;
    // 加载页面数据
    _this.getPageData();
    // 设置navbar标题、颜色
    App.setNavigationBar();

    // 获取默认门店列表
    _this.getShopList();
  },
  onShow(){
    App.pageOnLoad(this);
  },
  getPageData: function(callback) {
    let _this = this;
    App._get('page/index', {
      page_id: _this.data.page_id
    }, function(result) {
      // 设置顶部导航栏栏
      _this.setPageBar(result.data.page);
      _this.setData(result.data);
      var items = result.data.items;
      var _items1 = {};
      var _items2 = {};
      for (const key in items) {
        if (items[key]['type']=='video') {
          _items2[key] = items[key];
        }
        else{
          _items1[key] = items[key];
        }
      }
      //console.log(items2);
      _this.setData({items1:_items1, items2:_items2});
      // 回调函数
      typeof callback === 'function' && callback();
    });
  },
  getShopList(longitude, latitude) {
    let _this = this;
    _this.setData({
      isLoading: true
    });
    App._get('shop/lists', {
      longitude: longitude || '',
      latitude: latitude || ''
    }, (result) => {
      _this.setData({
        shopList: result.data.list,
        isLoading: false
      });
    });
  },
  gotoShopDes(e){
    wx.navigateTo({
      url: '/pages/shop/detail/index?shop_id=' + e.currentTarget.dataset.id,
    });
  },
  onOpenLocation(e) {
    var cnt = e.currentTarget.dataset.cnt;
    let _this = this,
      detail = _this.data.shopList[cnt];
    wx.openLocation({
      name: detail.shop_name,
      address: detail.region.province + detail.region.city + detail.region.region + detail.address,
      longitude: Number(detail.longitude),
      latitude: Number(detail.latitude),
      scale: 15
    });
  },
  /**
   * 设置顶部导航栏
   */
  setPageBar: function(page) {
    // 设置页面标题
    wx.setNavigationBarTitle({
      title: page.params.title
    });
    // 设置navbar标题、颜色
    wx.setNavigationBarColor({
      frontColor: page.style.titleTextColor === 'white' ? '#ffffff' : '#000000',
      backgroundColor: page.style.titleBackgroundColor
    })
  },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function() {
    // 获取首页数据
    this.getPageData(function() {
      wx.stopPullDownRefresh();
    });
  },
  /**
   * 分享当前页面
   */
  onShareAppMessage: function() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams({
      'page_id': _this.data.page_id
    });
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/shop/index?" + params
    };
  },
  /**
   * 分享到朋友圈
   * 本接口为 Beta 版本，暂只在 Android 平台支持，详见分享到朋友圈 (Beta)
   * https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share-timeline.html
   */
  onShareTimeline: function() {
    const _this = this;
    // 构建页面参数
    const params = App.getShareUrlParams({
      'page_id': _this.data.page_id
    });
    return {
      title: _this.data.page.params.share_title,
      path: "/pages/shop/index?" + params
    };
  },
  /** 监听视频播放 */
  videoPlayEvent(e){
    var vid = e.target.dataset.vid;
    console.log(vid);
    for (let key in this.data.items2) {
      if(key!=vid){
        const videoCtx = wx.createVideoContext('vedio_'+key);
        if(videoCtx!=undefined){
          videoCtx.pause();
        }
      }
    }
  }
});